import { queryUserInfoApi, visitorApi } from "@/servers/userServer.js";
import storage from "@/utils/storage.js";

const userStore = {
    namespaced: true,
    state: {
        userInfo: {}, 

    },
    mutations: {
        getUserInfo(state, data) {
            state.userInfo = data;
        },
    },
    actions: {
        async getUserInfoAction(context){
            const res = await visitorApi()
            const {insertId} = res.data
            const userInfoRes = await queryUserInfoApi({user_id:insertId})
            console.log(" userInfoRes.data" , userInfoRes.data);
            context.commit("getUserInfo" , userInfoRes.data)
            storage.setStorage("userInfo" , userInfoRes.data)
        },
        // 调用网络请求更新userInfo
        async updateUserInfoAction(context , user_id){
            const userInfoRes = await queryUserInfoApi({user_id})
            console.log(" userInfoRes.data" , userInfoRes.data);
            context.commit("getUserInfo" , userInfoRes.data)
            storage.setStorage("userInfo" , userInfoRes.data)
        },
        // 直接更新userInfo
        updateUserInfoFn(context , userInfo){
            console.log("userInfo" , userInfo);
            context.commit("getUserInfo" , userInfo)
            storage.setStorage("userInfo" , userInfo)
        }
    }
}
export default userStore